#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void swap(char*, char*);

char* get_mid_member(char* first, char* second, char* last)
{
    if (*first > *second) {
        swap(first, second);
	}
    if (*last < *second) {
    	swap(last, second);
    }
	if (second < first) {
		return first;
	}
    return second;
}

void swap(char * one, char * two)
{
	char tmp = *one;
	*one = *two;
	*two = tmp;
}

char * quick_sort(char * array, int first, int end)
{
    if (NULL == array) {
        printf("Error, it is a empty array\n");
		return NULL;
	}
	if (first < end) {
	int i = first+1, j = end;
	//char *mid = get_mid_member(array+first, array+((first+end)/2), array+end);
	//swap(mid, array+end);
	while (1) {
	    while (array[i] < array[first] && i < end) i++;
	    while (array[j] > array[first] && j > 0) j--;
		if (i<j) {
            swap(array+i, array+j);
			i++; j--;
		}
		else {
            break;
		}
	}
    swap(array+j, array+first);
	quick_sort(array, first, j-1);
	quick_sort(array, i, end);
	}
}

int main(int argc, char * argv[]) 
{
    //char array[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
	//quick_sort(array, 0, 9);
	//for (int i=0; i<10; ++i) {
	char array[] = {1, 3, 2, 44};
	quick_sort(array, 0, 3);
	for (int i=0; i<4; ++i) {
        printf("%d ", array[i]);
	}
	printf("\n");
	return 0;
}

